package com.tcm.portal.web.controller;


import cn.hutool.core.util.ObjectUtil;
import com.base.core.exception.ServiceException;
import com.base.core.utils.R;
import com.tcm.content.dto.AppraisesDTO;
import com.tcm.content.service.AppraisesService;
import com.tcm.content.service.PanelDoctorService;
import com.tcm.content.service.PanelNavService;
import com.tcm.content.service.PanelSlideService;
import com.tcm.doctor.domain.dto.DoctorDTO;
import com.tcm.doctor.service.DoctorService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RequestMapping("/portal")
@RestController
public class AppraisesController {

    @DubboReference(version = "0.0.1-alpha", timeout = 6000, check = false, retries = 3)
    private AppraisesService appraisesService;

    @DubboReference(version = "0.0.1-alpha", timeout = 6000, check = false, retries = 3)
    private DoctorService doctorService;

    @GetMapping("/appraises/list")
    public R listAppraises(@RequestParam("userId") Long userId) {
        Map<String, Object> rs = new HashMap<>();
        rs.put("totalCount", appraisesService.countAppraises(userId));
        rs.put("goodRate", "99%");
        rs.put("appraises", appraisesService.listAppraises(userId, 10));
       return R.success(rs);
    }

    /**
     * 全部评价列表
     * @return
     */
    @GetMapping("/appraises/list/page")
    public R listPageAppraises(@RequestParam("userId") Long userId,
                               @RequestParam(defaultValue = "1") Long page,
                               @RequestParam(defaultValue = "10") Long pagesize) {
        Map<String, Object> rs = new HashMap<>();
        rs.put("goodRate", "99%");
        DoctorDTO doctorDTO = doctorService.findDoctorByUserId(userId);
        if(ObjectUtil.isNotEmpty(doctorDTO)){
            rs.put("doctorAvatar", doctorDTO.getAvatar());
            rs.put("doctorName", doctorDTO.getName());
            rs.put("doctorJobtitle", doctorDTO.getJobtitle());
        }else{
            rs.put("doctorAvatar", "");
            rs.put("doctorName", "");
            rs.put("doctorJobtitle", "");
        }
        rs.put("appraises", appraisesService.listPageAppraises(userId, page, pagesize));
        return R.success(rs);
    }

    @PostMapping("/appraises/save")
    public R saveAppraises(@RequestBody AppraisesDTO appraisesDTO) {
        try {
            appraisesService.save(appraisesDTO);
        }catch (ServiceException e){
            return R.fail(e.getResult().getCode(), e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
        return R.success();
    }



}

